home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 11 / Cream of the Crop 11-1.iso / games / text_eng.zip / GLOBE.C < prev    next >
C/C++ Source or Header  |  1995-12-12  |  10KB  |  355 lines

  1.  
  2.  
  3.  
  4. /*
  5.  
  6. First off, we need code to draw an ELLIPSE, Using X,Y co-ords...
  7.  
  8.  
  9.  
  10. */
  11.  
  12. #include <stdio.h>
  13. #include <stdlib.h>
  14. #include <math.h>
  15. #include <dos.h>
  16. #include "gl.h"
  17.  
  18. unsigned char ibuffer[70000];
  19.  
  20. #define M_PI        3.14159265358979323846
  21.  
  22. int sin256[360];
  23. int sin1024[1024];
  24. /*
  25. = { 0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,
  26. 8,8,9,9,9,10,10,11,11,12,12,12,13,13,13,14,14,14,15,
  27. 15,15,16,16,16,17,17,17,17,18,18,18,18,18,19,19,19,
  28. 19,19,19,20,20,20,20,20,20,20,20,20,20,20,20,20,20,
  29. 20,20,20,20,20,19,19,19,19,19,19,18,18,18,18,18,17,
  30. 17,17,17,16,16,16,15,15,15,15,14,14,13,13,13,12,12,
  31. 12,11,11,10,10,9,9,9,8,8,7,7,6,6,5,5,4,
  32. 4,3,3,2,2,2,1,1,0,0,0,0,-1,-1,-2,-2,-3,
  33. -3,-4,-4,-5,-5,-6,-6,-7,-7,-8,-8,-8,-9,-9,-10,-10,-10,
  34. -11,-11,-12,-12,-12,-13,-13,-13,-14,-14,-14,-15,-15,-15,-16,-16,-16,
  35. -16,-17,-17,-17,-17,-17,-18,-18,-18,-18,-18,-18,-19,-19,-19,-19,-19,
  36. -19,-19,-19,-19,-19,-19,-19,-19,-19,-19,-19,-19,-19,-19,-18,-18,-18,
  37. -18,-18,-18,-17,-17,-17,-17,-17,-16,-16,-16,-16,-15,-15,-15,-14,-14,
  38. -14,-14,-13,-13,-12,-12,-12,-11,-11,-11,-10,-10,-9,-9,-8,-8,-8,
  39. -7,-7,-6,-6,-5,-5,-4,-4,-3,-3,-2,-2,-2,-1,-1,0,0
  40.  }; 
  41. */
  42. int cos256[360];
  43. int cos1024[1024];
  44. /*
  45. = { 20,20,20,20,20,20,20,20,20,20,19,19,19,19,19,19,18,
  46. 18,18,18,18,17,17,17,17,16,16,16,15,15,15,14,14,14,
  47. 13,13,13,12,12,12,11,11,10,10,9,9,9,8,8,7,7,
  48. 6,6,5,5,4,4,3,3,2,2,1,1,1,0,0,0,0,
  49. -1,-1,-2,-2,-3,-3,-4,-4,-5,-5,-6,-6,-7,-7,-8,-8,-8,
  50. -9,-9,-10,-10,-10,-11,-11,-12,-12,-12,-13,-13,-13,-14,-14,-14,-15,
  51. -15,-15,-16,-16,-16,-16,-17,-17,-17,-17,-17,-18,-18,-18,-18,-18,-18,
  52. -19,-19,-19,-19,-19,-19,-19,-19,-19,-19,-19,-19,-19,-19,-19,-19,-19,
  53. -19,-19,-18,-18,-18,-18,-18,-18,-17,-17,-17,-17,-17,-16,-16,-16,-16,
  54. -15,-15,-15,-14,-14,-14,-14,-13,-13,-12,-12,-12,-11,-11,-11,-10,-10,
  55. -9,-9,-8,-8,-8,-7,-7,-6,-6,-5,-5,-4,-4,-3,-3,-2,-2,
  56. -1,-1,-1,0,0,0,0,1,1,2,2,3,3,4,4,5,5,
  57. 6,6,7,7,8,8,9,9,9,10,10,11,11,11,12,12,13,
  58. 13,13,14,14,14,15,15,15,16,16,16,17,17,17,17,18,18,
  59. 18,18,18,19,19,19,19,19,19,20,20,20,20,20,20,20,20,20
  60.  };
  61. */
  62.  
  63.  
  64. void make_sin(void)
  65. {
  66.     int i;
  67.     float a,c;
  68.  
  69.     c=M_PI/128.0;
  70.     for(i=0,a=0.0; i<256;i++,a+=c)
  71.     {
  72.         sin256[i]=(int)((sin(a)*256.0)+0.5);
  73.         cos256[i]=(int)((cos(a)*256.0)+0.5);
  74.     }
  75.     c=M_PI/512.0;
  76.     for(i=0,a=0.0; i<1024;i++,a+=c)
  77.     {
  78.         sin1024[i]=(int)((sin(a)*1024.0)+0.5);
  79.         cos1024[i]=(int)((cos(a)*1024.0)+0.5);
  80.     }
  81. }
  82.  
  83. long RotateX(int x,int y,int angle)
  84. {
  85.     return(((sin1024[angle]*y)-(cos1024[angle]*x))/1024);
  86. }
  87.  
  88. long RotateY(int x,int y,int angle)
  89. {
  90.     return(((sin1024[angle]*x)-(cos1024[angle]*y))/1024);
  91. }
  92.  
  93. #define SHAPESIZE 190
  94.  
  95. unsigned char   ShapeBuffX[SHAPESIZE*SHAPESIZE],
  96.                 ShapeBuffY[SHAPESIZE*SHAPESIZE],
  97.                 ShapeBuffZ[SHAPESIZE*SHAPESIZE];
  98.  
  99. void Globe_Ellipse(unsigned char *gbuffX,unsigned char *gbuffY,
  100.         unsigned char *gbuffZ,int cx,int cy,int rx,int ry,int rangle,int row)
  101. {
  102.     int i,j,k,x,y,fx,fy;
  103.  
  104.     for(i=0;i<1024;i++)   /* Drawing of "upper" portion always begins at 0 */
  105.     {
  106.         y=(cos1024[i]*ry)/16;  /* -127 to 127 */
  107.         x=(sin1024[i]*rx)/16;  /* 8.6 fixed point.. */
  108.         /* Now.. rotate and translate... */
  109.         fx=cx+(RotateX(x,y,rangle)>>6); if(fx<0) fx=0; if(fx>127) fx=127;
  110.         fy=cy-(RotateY(x,y,rangle)>>6); if(fy<0) fy=0; if(fy>127) fy=127;
  111.  
  112.         fx>>=1; fy>>=1;
  113.         fx+=(fy<<6);
  114.         gbuffX[fx]=i;
  115.         gbuffY[fx]=row;
  116.         gbuffZ[fx]=1;  /* mask temp buff */
  117.     }
  118. }
  119.  
  120. void YMake_Globe(float incr)
  121. {
  122.     float axis,xr,yr,xc,yc,el1,nx,ny;
  123.  
  124.     xc=160.0;
  125.     for(axis=0.0;axis<1.0;axis+=incr)
  126.     {
  127.         yr=sin((M_PI*axis)); /* 0.0...1.0...0.0 */
  128.         xr=yr*100.0;
  129.         yr*=10.0;            /* Now, XR and YR are set... */
  130.         yc=100.0+(cos((M_PI*axis))*90.0);
  131.         for(el1=0.0;el1<=2.0;el1+=0.001)
  132.         {
  133.             nx=(sin(M_PI*el1)*xr)+xc;
  134.             ny=(cos(M_PI*el1)*yr)+yc;
  135.             GL_SetPixel((int)(nx+0.5),(int)(ny+0.5),(int)(axis*256.0));
  136.         }
  137.         if(kbhit())
  138.             break;
  139.     }
  140.     while(kbhit()) getch();
  141. }
  142.  
  143. void XMake_Globe(float incr)
  144. {
  145.     float axis,xr,yr,xc,yc,el1,nx,ny;
  146.  
  147.     xc=160.0;
  148.     for(axis=0.0;axis<1.0;axis+=incr)
  149.     {
  150.         yr=sin((M_PI*axis)); /* 0.0...1.0...0.0 */
  151.         xr=yr*100.0;
  152.         yr*=10.0;            /* Now, XR and YR are set... */
  153.         yc=100.0+(cos((M_PI*axis))*90.0);
  154.         for(el1=0.0;el1<=2.0;el1+=0.001)
  155.         {
  156.             nx=(sin(M_PI*el1)*xr)+xc;
  157.             ny=(cos(M_PI*el1)*yr)+yc;
  158.             GL_SetPixel((int)(nx+0.5),(int)(ny+0.5),(int)(el1*128.0));
  159.         }
  160.         if(kbhit())
  161.             break;
  162.     }
  163.     while(kbhit()) getch();
  164. }
  165.  
  166. #if 0
  167. void Make_Globe(void)
  168. {
  169.     int row,xr,yr,yc,xc;
  170.     int i,j,k,x,y,fx,fy,rangle;
  171.  
  172.     rangle=0;
  173.     for(row=0;row<100;row++)
  174.     {
  175.         yc=((cos1024[(row*512)/50]*25)/1024)+25;
  176.         yc*=2;
  177.         xr=(sin1024[(row*512)/50]*32)/1024;
  178.         xr*=2;
  179.         yr=(sin1024[(row*512)/50]*5)/1024;
  180.         yr*=2;
  181.         /* Globe_Ellipse(ShapeBuffX,ShapeBuffY,ShapeBuffZ,64,yc+14,xr,yr,0,row/2); */
  182.         for(i=0;i<1024;i++)   /* Drawing of "upper" portion always begins at 0 */
  183.         {
  184.             y=(cos1024[i]*yr)/16;  /* -127 to 127 */
  185.             x=(sin1024[i]*xr)/16;  /* 8.6 fixed point.. */
  186.             /* Now.. rotate and translate... */
  187.             fx=xc+(RotateX(xr,yr,rangle)>>6); if(fx<0) fx=0; if(fx>127) fx=127;
  188.             fy=yc-(RotateY(xr,yr,rangle)>>6); if(fy<0) fy=0; if(fy>127) fy=127;
  189.  
  190.             fx>>=1; fy>>=1;
  191.             fx+=(fy<<6);
  192.             ShapeBuffX[fx]=i;
  193.             ShapeBuffY[fx]=row;
  194.             ShapeBuffZ[fx]=1;  /* mask temp buff */
  195.         }
  196.     }
  197. }
  198. #endif
  199.  
  200.  
  201.  
  202. void Make_Globe(float incr)
  203. {
  204.     float axis,xr,yr,xc,yc,el1,nx,ny,ShapeSize,RSize;
  205.     int fx,fy;
  206.  
  207.     ShapeSize=SHAPESIZE;
  208.     RSize=ShapeSize*0.44;
  209.     xc=ShapeSize/2.0;
  210.     for(axis=0.0;axis<1.0;axis+=incr)
  211.     {
  212.         yr=sin((M_PI*axis)); /* 0.0...1.0...0.0 */
  213.         xr=yr*RSize;
  214.         yr*=ShapeSize*0.1;            /* Now, XR and YR are set... */
  215.         yc=xc-(cos((M_PI*axis))*RSize);
  216.         for(el1=0.0;el1<=2.0;el1+=incr)
  217.         {
  218.             nx=((sin(M_PI*el1)*xr)+xc)+0.5;
  219.             ny=((cos(M_PI*el1)*yr)+yc)+0.5;
  220.             fx=(int)nx; fy=(int)ny;
  221.             if(fx>=SHAPESIZE) fx=SHAPESIZE-1; if(fy>=SHAPESIZE) fy=SHAPESIZE-1;
  222.             fx+=(fy*SHAPESIZE);
  223.             ShapeBuffX[fx]=255-(int)(el1*256.0);
  224.             ShapeBuffY[fx]=(int)(axis*256.0);
  225.             ShapeBuffZ[fx]=1;  /* mask temp buff */
  226.         }
  227.     }
  228. }
  229.  
  230.  
  231. void XXMake_Globe(void)
  232. {
  233.     int row,xr,yr,yc;
  234.  
  235.     for(row=0;row<100;row++)
  236.     {
  237.         yc=((cos1024[(row*512)/50]*25)/1024)+25;
  238.         yc*=2;
  239.         xr=(sin1024[(row*512)/50]*32)/1024;
  240.         xr*=2;
  241.         yr=(sin1024[(row*512)/50]*5)/1024;
  242.         yr*=2;
  243.         Globe_Ellipse(ShapeBuffX,ShapeBuffY,ShapeBuffZ,64,yc+14,xr,yr,0,row/2);
  244.     }
  245. }
  246.  
  247. unsigned char ipal[768];
  248. unsigned char opal[768];
  249.  
  250. void SCALE1( int X, int Y, int DW, int DY,int SW, int SH, unsigned char * Bitmap );
  251.  
  252. void main(void)
  253. {
  254.     int x,y,i,j,xr,yr,yc,nx,ny,ox,oy;
  255.  
  256. #if 0
  257.     make_sin();
  258.     x=RotateX(10,0,0);
  259.     y=RotateY(10,0,0);
  260.     printf("(10,0) Rotated: %d  X:%d  Y:%d \n",0,x,y);
  261.     x=RotateX(10,0,40);
  262.     y=RotateY(10,0,40);
  263.     printf("(10,0) Rotated: %d  X:%d  Y:%d \n",40,x,y);
  264.     x=RotateX(10,0,64);
  265.     y=RotateY(10,0,64);
  266.     printf("(10,0) Rotated: %d  X:%d  Y:%d \n",64,x,y);
  267.     x=RotateX(10,0,80);
  268.     y=RotateY(10,0,80);
  269.     printf("(10,0) Rotated: %d  X:%d  Y:%d \n",80,x,y);
  270.     x=RotateX(10,0,170);
  271.     y=RotateY(10,0,170);
  272.     printf("(10,0) Rotated: %d  X:%d  Y:%d \n",170,x,y);
  273.     getch();
  274. #endif
  275.     printf("Please wait...\n");
  276.     Make_Globe(0.003);
  277.     GL_InitMode13();
  278.     GL_Gif("bb256256.gif",ibuffer,ipal,&i,&j);
  279.     /*
  280.     YMake_Globe(0.005);
  281.     getch();
  282.     XMake_Globe(0.005);
  283.     getch();
  284.     */
  285. #if 0
  286.     for(j=0;j<256;j++)
  287.     {
  288.         xr=sin1024[j*2];
  289.         if(j<128)
  290.         {
  291.             yr=(j*8);
  292.         }
  293.         else
  294.         {
  295.             yr=(128-(j%128))*8;
  296.         }
  297.         yc=((cos1024[(j*512)/256]*128)/1024)+128;
  298.         for(i=0;i<1024;i++)
  299.         {
  300.             x=100+((RotateX(0,100,i)*xr)/1024);
  301.             y=10+((yc*180)/256)+((RotateY(0,80,i)*yr)/1024);
  302.             GL_SetPixel(x,y,j);
  303.         }
  304.         if(kbhit())
  305.             break;
  306.     }
  307.     getch();
  308.     x=0;
  309.     for(i=0;i<128;i++)
  310.     {
  311.         for(j=0;j<128;j++)
  312.         {
  313.             if(ShapeBuffZ[x])
  314.             {
  315.                 GL_SetPixel(j,i,ShapeBuffY[x]);
  316.                 GL_SetPixel(100+j,i,ShapeBuffX[x]);
  317.             }
  318.             x++;
  319.         }
  320.     }
  321.     getch();
  322. #endif
  323.     memset(opal,0,768);
  324.     GL_FadeInPal(opal,2000);
  325.     GL_ClearScreenColor(GL_FindColor(ipal,0,0,0,9999));
  326.     SCALE1(0,0,320,200,256,256,ibuffer);
  327.     GL_FadeInPal(ipal,2000);
  328.     yc=0;
  329.     ox=(320-SHAPESIZE)/2;
  330.     oy=(200-SHAPESIZE)/2;
  331.     while(!kbhit())
  332.     {
  333.         x=0;
  334.         for(i=0;i<SHAPESIZE;i++)
  335.         {
  336.             for(j=0;j<SHAPESIZE;j++)
  337.             {
  338.                 if(ShapeBuffZ[x])
  339.                 {
  340.                     nx=(ShapeBuffX[x]+yc)%256;
  341.                     ny=ShapeBuffY[x]<<8;
  342.                     GL_SetPixel(ox+j,oy+i,ibuffer[nx|ny]);
  343.                 }
  344.                 x++;
  345.             }
  346.         }
  347.         yc++; yc%=256;
  348.     }
  349.     getch();
  350.     GL_TextMode();
  351. }
  352.  
  353.  
  354.  
  355.